<?php
$fruitsInfo=array("apples"=>0.69,"oranges"=>0.59,"bananas"=>0.39);
$paymentInfo=array("Visa","MasterCard","Discover");
$fruitsQuanAdd=array();
$totalCost=0;
function test_input($data){
    $data=trim($data);
    $data=stripslashes($data);
    $data=htmlspecialchars($data);
    return $data;
}
function test_fruit_input($data){
    if ($data=="") return true;
    return ctype_digit($data);
}
function alert_and_redirect($message,$url){
    echo "<SCRIPT type='text/javascript'>alert(\"$message\");window.location.href=\"$url\";</SCRIPT>";
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>receipt</title>
    <meta name="description" content="">
    <meta name="author" content="Chen Peng">
    <link rel="stylesheet" href="uikit/css/uikit.min.css">
</head>
<body>
<div class="uk-panel uk-panel-box uk-width-2-5 uk-container-center uk-margin-large-top" id="main-panel">
<table class="uk-table">
    <tbody>
    <tr id="username">
        <td>Username</td>
        <td><?php 
            $username=test_input($_POST["username"]);
            if ($username==""){
                alert_and_redirect("No Username","index.html");
            }else{
                echo $username;
            }
            ?>
        </td>
    </tr>
    <tr>
        <td>Payment method</td>
        <td><?php
            $payment=test_input($_POST["payment"]);
            if(!in_array($payment,$paymentInfo)){
                alert_and_redirect("Missing or not accepted payment method","index.html");
            }else{
                echo $payment;
            }
            ?>
        </td>
    </tr>

    <?php 
    foreach ($fruitsInfo as $fruit=>$price){
        $quantity=test_input($_POST[$fruit]);
        if(!test_fruit_input($quantity)){
            alert_and_redirect("The quantity of $fruit is not valid","index.html");
        }else{
            echo "<tr class=\"fruit\" id=$fruit><td>" . $fruit . "</td><td>" . $quantity . "</td></tr>";
            $fruitsQuanAdd[$fruit]=$quantity;
            $totalCost=$totalCost+$quantity*$price;
        }
    }
    ?>
    </tbody>
    <tfoot>
    <tr id="total">
        <td>Total</td>
        <td><?php 
            if($totalCost==0){
                alert_and_redirect("Please order some fruits","index.html");
            }else{
                echo $totalCost;
            }
            ?>
        </td>
    </tr>
    </tfoot>
</table>
</div>
<?php
    $filename="order.txt";
    if(!file_exists($filename)){
        $fptr = fopen($filename, 'w')
            or exit("Unable to create file \"".$filename."\"");
        foreach($fruitsInfo as $fruit=>$price){
            fwrite($fptr, "Total number of ".$fruit.": 0\n");
        }
        fclose($fptr);
    }
    $lines= file($filename);
    foreach($lines as &$l){
        sscanf($l,"Total number of %s %d",$f,$q);
        $f=chop($f,":");
        $l= preg_replace("([0-9]+)", $q+$fruitsQuanAdd[$f], $l);
    }
    unset($l);
    file_put_contents($filename,$lines);
?>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="uikit/js/uikit.min.js"></script>
</body>
</html>
